import moment from "moment";

let socketRoot = null;

const socketHeart = (vm) => {
    clearInterval(vm.heart);
    vm.heart = null;
    vm.heart = setInterval(() => {
        if (!vm.conn) {
            console.log("通讯中断重连 ... ...")
            conWebSocket(vm, { port: 8206 });
        } else {
            vm.socket.send(
                JSON.stringify({
                    uid: vm.uid,
                    type: "heart",
                })
            );
        }
    }, 10000);
}

const conWebSocket = (vm, serverConfig = {host:"ws://192.168.0.180",port:8206}) => {
    if (window.WebSocket) {
        vm.socket = new WebSocket(serverConfig.host +":"+ serverConfig.port);
        // vm.socket = new WebSocket('ws://192.168.0.180:' + serverConfig.port);
        let socket = vm.socket;
        socketRoot = socket;
        socket.onopen = () => {
            console.log("通讯服务器连接成功");
            vm.conn = true;
            if (!vm.uid) {
                // 生成新的用户id,并存入localStorage
                vm.uid = 'web_im_' + moment().valueOf();
                localStorage.setItem('WEB_IM_USER', JSON.stringify({
                    uid: vm.uid,
                    nickname: vm.nickname
                }))
                vm.sendMessageConn(1);
            }
        };
        socket.onclose = () => {
            console.log("通讯服务器关闭");
            vm.conn = false;
        };
        socket.onerror = () => {
            vm.conn = false;
            console.log("通讯连接出错");
        };
        // let {userInfo, toUserInfo} = vm.state;
        // 接收服务器的消息
        // socket.onmessage = (e) => {
        //     let message = JSON.parse(e.data);
        //     if (vm.uid === message.uid || vm.uid !== message.uid) {
        //         vm.cargoMsg = message;
        //         let {msgList} = vm.state;
        //         if (message.type === "letter" && vm.callMsgAlert)
        //             vm.callMsgAlert();
        //         else if (message.type === "chat" && msgList){
                   
        //             // 与自己无关的消息
        //             let fm = (message.fromUid === userInfo.uid && message.toUid === toUserInfo.uid);
        //             let tm = (message.fromUid === toUserInfo.uid  && message.toUid === userInfo.uid);
        //             console.log(1,fm)
        //             console.log(2,tm)
        //             console.log("message-f",message.fromUid)
        //             console.log("message-t",message.toUid)
        //             console.log("userInfo",userInfo.uid)
        //             console.log("toUserInfo",toUserInfo.uid)
        //             // if(!fm || !tm) return;

        //             message.mid = Date.now();
        //             msgList.push(message);
        //             vm.setState({msgList});
        //             setTimeout(()=>{
        //                 vm.msglistWarp.scrollTo({
        //                     top:vm.msglistWarpInner.offsetHeight - vm.msglistWarp.clientHeight,
        //                     behavior:"smooth",
        //                 });
        //             },50)
        //         }
        //     }
        // };
    };
    socketHeart(vm); // 启动心跳包
};

const getSocket = ()=>{
    return socketRoot;
}

export { conWebSocket,getSocket }

const msg = {
    type: "letter | chat | heart",
    from: "",
    to: "",
    content: "文本"
}

/* Readme:


*/